<template>
  <el-row>
    <el-col :span="6">
      <div class="dept-tree">
        <el-input
          style="width:90%;margin: 10px 15px;"
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          size="small"
          v-model="filterText"
        ></el-input>
        <el-tree
          ref="tree"
          :data="treeData"
          :indent="0"
          node-key="id"
          default-expand-all
          :expand-on-click-node="true"
          :highlight-current="true"
          :filter-node-method="filterNode"
          :props="{children: 'children',label: 'title'}"
          @current-change="currentChange"
          @node-mouse-over="append(1)"
        >
          <span
            class="custom-tree-node"
            slot-scope="{ node, data }"
            :class="{'custom-tree-node_first-child':data.id === 0}"
          >
            <span>{{ node.label }}</span>
            <span class="btn">
              <el-button type="text" size="mini" @click.stop="() => append(node,data)">
                <i class="el-icon-plus"></i>
              </el-button>
              <el-button type="text" size="mini" @click="() => edit(node, data)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button type="text" size="mini" @click="() => remove(node, data)">
                <i class="el-icon-delete"></i>
              </el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </el-col>
    <el-col :span="18">
      <div class="dept-content">
        <div class="dept-content_header">头部</div>
        <div class="dept-content_body">
          <el-form :inline="true" :model="search" class="demo-form-inline">
            <el-form-item>
              <el-input v-model="search.key" size="small" placeholder="支持搜索账号、昵称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="search.mobile" size="small" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="search.status" size="small" placeholder="请选择状态"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" icon="el-icon-search">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-search">重置</el-button>
            </el-form-item>
          </el-form>
          <div>
            <el-button type="primary" size="small">添加</el-button>
            <el-button type="primary" size="small">导出</el-button>
            <el-button type="primary" size="small">导入</el-button>
          </div>
          <el-table :data="tableData" style="width: 100%;height:100%;margin-top:10px;" border>
            <el-table-column type="index" label="序号" width="80"></el-table-column>
            <el-table-column prop="username" label="账号" width="100"></el-table-column>
            <el-table-column prop="name" label="昵称" width="100"></el-table-column>
            <el-table-column prop="deptName" label="部门" width="100"></el-table-column>
            <el-table-column prop="roleName" label="角色" width="100"></el-table-column>
            <el-table-column prop="mobile" label="手机号" width="140"></el-table-column>
            <el-table-column prop="email" label="邮箱" width="150"></el-table-column>
            <el-table-column prop="gender" label="性别"></el-table-column>
            <el-table-column prop="awatar" label="头像" width="150"></el-table-column>
            <el-table-column prop="status" label="状态" width="100"></el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
              <template slot-scope="scope">
                <el-button
                  @click.native.prevent="deleteRow(scope.$index, tableData)"
                  type="text"
                  size="small"
                >移除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
export default {
  data () {
    return {
      filterText: '',
      search: {},
      tableData: [],
      treeData: []
    }
  },
  created () {
    this.menuList()
  },
  methods: {
    append (node, data) {
      console.log(node, data)
    },
    edit (node, data) {
      console.log(node, data)
    },
    remove (node, data) {
      console.log(node, data)
    },
    // 树形菜单过滤搜索
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 选中节点时，触发函数
    currentChange (data, node) {
      if (node.isLeaf) {
        // 如果是叶子节点，就需要查询接口
        console.log('调用接口。。。。。。')
      }
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  }
}
</script>

<style lang="scss" scoped>
.dept {
  &-tree,
  &-content {
    min-height: 85vh;
    background-color: #fff;
  }
  &-tree {
    border-radius: 5px;
  }
  &-content {
    padding: 20px 8px 0px 10px;
    margin-left: 10px;
    &_header {
      min-height: 30vh;
      background-color: pink;
    }
    &_body {
      min-height: calc(100% - 30vh);
    }
  }
}
.el-tree {
  padding-left: 15px;

  ::v-deep .el-tree-node {
    position: relative;
    padding-left: 4px; // 缩进量
  }

  ::v-deep .el-tree-node__children {
    padding-left: 4px; // 缩进量
  }
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;

  .btn {
    visibility: hidden;
  }
}

.custom-tree-node_first-child {
  color: #474444;
  font-size: 15px;
  font-weight: 700;
}

// 鼠标悬浮显示按钮
.el-tree-node__content:hover .btn {
  visibility: visible;
}
</style>
